package com.hank.snakegame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GamePanel extends JPanel {

    public static final int CELL_UNIT =20;
    //x坐标
    public static final int ROW_NUM = 30;
    //y坐标
    public static final int COL_NUM = 40;

    private Cell[][] panelCells = new Cell[ROW_NUM][COL_NUM];

    private Snake snake= null;
    private Food food = null;

    //init初始化
    private void init(){
        for (int i = 0; i < panelCells.length; i++) {
            for (int j = 0; j < panelCells[i].length ; j++) {
                panelCells[i][j] = new Cell(j,i, Color.WHITE);
            }
        }
    }

    private void paintPanelCells(Graphics g){
        for(int i=0;i<panelCells.length;i++){
            for(int j =0;j<panelCells[i].length;j++){
                panelCells[i][j].paint(g);
            }
        }
    }

    public GamePanel() {init();}

    @Override
    public void paint(Graphics g){
        super.paint(g);
        paintPanelCells(g);
        snake.paint(g);
        food.paint(g);
    }

    public static void main(String[] args) {new GamePanel().start();}

    public void start(){
        JFrame jFrame = new JFrame("贪吃蛇v1.0");
        jFrame.setSize(800,630);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        GamePanel gamePanel = new GamePanel();
        gamePanel.snake = new Snake();
        gamePanel.food = Food.generateFood(gamePanel.snake.getSnakeCells());
        jFrame.add(gamePanel);

        //监听键盘操作  抽象类KeyAdapter
        jFrame.addKeyListener(new MyKeyLinenter(jFrame,gamePanel));

        jFrame.setVisible(true);
    }

    //KeyAdapter implements KeyListener
    public class MyKeyLinenter extends KeyAdapter{
        private GamePanel gamePanel;
        private JFrame jFrame;

        public MyKeyLinenter( JFrame jFrame,GamePanel gamePanel) {
            this.gamePanel = gamePanel;
            this.jFrame = jFrame;
        }

        @Override
        public void keyReleased(KeyEvent e){
            super.keyReleased(e);
            boolean canMove = false;
            int keyCode = e.getKeyCode();
            switch (keyCode){
                case KeyEvent.VK_LEFT:
                    System.out.println("向左");
                    canMove = gamePanel.snake.mobeHead(-1,0);
                    break;
                case KeyEvent.VK_RIGHT:
                    System.out.println("向右");
                    canMove = gamePanel.snake.mobeHead(1,0);
                    break;
                case KeyEvent.VK_UP:
                    System.out.println("向上");
                    canMove = gamePanel.snake.mobeHead(0,-1);
                    break;
                case KeyEvent.VK_DOWN:
                    System.out.println("向下");
                    canMove = gamePanel.snake.mobeHead(0,1);
                    break;
            }

            if (!canMove){
                return;
            }

            //咬一口
            boolean eatFoodSuccess =gamePanel.snake.eatFood(gamePanel.food);
            if(eatFoodSuccess){
                //吃到了
                gamePanel.food = Food.generateFood(gamePanel.snake.getSnakeCells());
            }else{
                gamePanel.snake.removeTail();
            }

            jFrame.repaint();
        }
    }
}
